//
//  LaunchViewController.m
//  XiXi
//
//  Created by HawkingWang on 16/9/29.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "LaunchViewController.h"
#import "WXApi.h"//微信Api

#import "LoginViewController.h"

@interface LaunchViewController () <WXApiDelegate>

@property (weak, nonatomic) IBOutlet UIView *loginView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *managerLoginBtn;
@property (weak, nonatomic) IBOutlet UIImageView *wechat;
@property (weak, nonatomic) IBOutlet UIButton *wechatLoginBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leading;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top;


@end

@implementation LaunchViewController
{
    UIView *_bgView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    _loginView.hidden = YES;
    _loginView.alpha = 0;
    
    [self setLayer];
    
    [self launchAnimation];
    
}


- (void)setLayer{
    _label.layer.masksToBounds = YES;
    _label.layer.cornerRadius = 2;
    _label.layer.borderWidth = 0.5;
    _label.layer.borderColor = [UIColor whiteColor].CGColor;
    
    _managerLoginBtn.layer.masksToBounds = YES;
    _managerLoginBtn.layer.cornerRadius = 22;
    _managerLoginBtn.layer.borderWidth = 0.5;
    _managerLoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    
    _wechat.layer.cornerRadius = 45.5;
    _wechat.layer.masksToBounds = YES;
    
    _wechatLoginBtn.layer.cornerRadius = 50;
    _wechatLoginBtn.layer.masksToBounds = YES;
    
}


- (void)launchAnimation{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:_bgView];
    
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgImage.image = [UIImage imageNamed:@"bg"];
    [_bgView addSubview:bgImage];
    
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    blackView.backgroundColor = [UIColor blackColor];
    [_bgView addSubview:blackView];
    
    UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 138) / 2, 160, 138, 91)];
    logoImage.alpha = 0.3;
    logoImage.image = [UIImage imageNamed:@"logo-big"];
    [_bgView addSubview:logoImage];
    
    UIView *hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    hiddenView.backgroundColor = [UIColor blackColor];
    [_bgView addSubview:hiddenView];

    
    [UIView animateWithDuration:2.5 animations:^{
        blackView.alpha = 0.6;
        logoImage.alpha = 1;
        hiddenView.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
                    _bgView.alpha = 0.01;
        }];
        
        [UIView animateWithDuration:0.5 animations:^{
            _loginView.alpha = 1;
        }];
    }];
}


//微信授权登录
- (IBAction)touchDown:(id)sender {
    DLog(@"微信授权登录");
    _leading.constant = 0;
    _top.constant = 0;
    _wechat.layer.cornerRadius = 50;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _leading.constant = 4.5;
        _top.constant = 4.5;
        _wechat.layer.cornerRadius = 45.5;
        if ([WXApi isWXAppInstalled]) {
            SendAuthReq *req = [[SendAuthReq alloc] init];
            req.scope = @"snsapi_userinfo";
            req.state = @"123";
            [WXApi sendReq:req];
        }else{
            [self setupAlertController];
        }
    });
}


//设置弹出提示语
- (void)setupAlertController{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先安装微信客户端" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}


//管理员登录
- (IBAction)managerLogin:(id)sender {
    LoginViewController *login = [[LoginViewController alloc] init];
    [self presentViewController:login animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
